package com.comsumer.myribbon.controller;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.*;
import myrule.DecodeServerChooseRule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * HelloConsumerController
 * <p/>
 *
 * @author zhangshuai
 * @version 1.0
 * @date 2021-5-8 10:28
 **/
@RestController
public class HelloConsumerController {
    @Autowired
    RestTemplate restTemplate;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    private DiscoveryClient discoveryClient;


    @Autowired
    SpringClientFactory springClientFactory;


    @GetMapping("/helloworld")
    public String helloWorld() {
        return restTemplate.getForEntity("http://RIBBON-SERVICE/hello", String.class).getBody();
    }

    @GetMapping("/instance")
    public List<ServiceInstance> showInfo() {
        return discoveryClient.getInstances("RIBBON-SERVICE");
    }

    @GetMapping("/getServices")
    public List<String> getServices() {
        return discoveryClient.getServices();
    }

    @GetMapping("/log-user-instance")
    public void logUserInstance() {
        ServiceInstance serviceInstance = loadBalancerClient.choose("RIBBON-SERVICE");
        //打印当前选择的是哪个节点
        System.out.println(serviceInstance.getServiceId() + serviceInstance.getHost() + ":" + serviceInstance.getPort());
    }

    @GetMapping("/rule")
    public ServiceInstance rule() {
        ServiceInstance serviceInstance = loadBalancerClient.choose("RIBBON-SERVICE");
        //打印当前选择的是哪个节点
        System.out.println(serviceInstance.getServiceId() + serviceInstance.getHost() + ":" + serviceInstance.getPort());
        return serviceInstance;
    }

    @GetMapping("/ribbonServer")
    public String ribbonServer() {

//        DynamicServerListLoadBalancer balancer = new DynamicServerListLoadBalancer();
//
//        balancer.
//
//        AbstractLoadBalancer abstractLoadBalancer = new AbstractLoadBalancer();
//        Server server = abstractLoadBalancer.chooseServer("ZL-ZHANGSHUAI:1112");
//
//        System.out.println("server:" + server);

        // ServiceInstance serviceInstance = ribbonLoadBalancerClient.choose("RIBBON-SERVICE", "key");

        //打印当前选择的是哪个节点
        //System.out.println(serviceInstance.getServiceId() + serviceInstance.getHost() + ":" + serviceInstance.getPort());



        return restTemplate.getForEntity("http://RIBBON-SERVICE/hello", String.class).getBody();
    }

}